package com.blockchain.securewallet.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by Lopez on 2018/5/25.
 */
public class SelectorTextView extends TextView {

    public OnTouchListener onTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            TextView tv = (TextView) v;

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    tv.setAlpha(0.1f);
                    break;
                case MotionEvent.ACTION_UP:
                    tv.setAlpha(1.0f);
                    //放开才触发点击事件，让用户知道
                    mPerformClick();
                    break;
                case MotionEvent.ACTION_CANCEL:
                    tv.setAlpha(1.0f);
                    break;
            }
            return true;
        }
    };

    public SelectorTextView(Context context) {
        super(context);

        this.setOnTouchListener(onTouchListener);
    }

    public SelectorTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SelectorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SelectorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void mPerformClick(){
        SelectorTextView.this.performClick();
    }

}
